#include "imageFile.h"
#include "jsonUtils.h"
#include "fileUtils.h"

void ImageFile::fromJson(const JsonValue& value)
{
	JsonUtils::loadLong(value, L"id", id);
	JsonUtils::loadLong(value, L"fileId", fileId);
	JsonUtils::loadStr(value, L"filePath", filePath);
}

void ImageFile::toJson(JsonValue& value, JsonDoc::AllocatorType& allocator, bool toConfig) const
{
	if (toConfig) {
		value.AddMember(L"id", id, allocator);
		value.AddMember(L"fileId", fileId, allocator);
		JsonUtils::addString(value, L"filePath", filePath, allocator);
	}
	else {
		String url = FileUtils::url(movieId, BOOK_IMAGE, id);
		JsonUtils::addString(value, L"url", url, allocator);
		JsonUtils::addString(value, L"filePathRel", filePathRelative2html, allocator);
	}
	
}

const int64_t& ImageFile::getId() const
{
	return id;
}

const String& ImageFile::getFilePath() const
{
	return filePath;
}

bool ImageFile::pathEndWith(const String& lastPath) const
{
	if (lastPath.empty()) {
		return false;
	}
	const Char* p = wcsstr(filePath.c_str(), lastPath.c_str());
	return p != NULL;
}

void ImageFile::setMovieId(const int64_t& movieId)
{
	this->movieId = movieId;
}

void ImageFile::relative2html(const String& htmlPath)
{
	this->filePathRelative2html = FileUtils::relativePath(filePath, htmlPath);
}
